package com.gdufe.order.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.gdufe.order.dto.CreateOrderDto;
import com.gdufe.order.service.OrderUserService;
import com.gdufe.order.vo.UserOrderDetilsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gdufe.common.utils.PageUtils;
import com.gdufe.common.utils.R;



/**
 * 
 *
 * @author honghu
 * @email 3027708213@qq.com
 * @date 2023-10-28 16:07:06
 */
@RestController
@Slf4j
@RequestMapping("/order/user")
public class OrderUserController {
    @Autowired
    private OrderUserService orderUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderUserService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 订单详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		UserOrderDetilsVo orderDetils = orderUserService.getOrderDetils(id);
        orderDetils.getStatus();
        return R.ok().put("order", orderDetils);
    }


    /**
     * 支付订单
     */
    @RequestMapping("/pay/{id}")
    @SentinelResource("pay")
    public R payOrder(@PathVariable("id") Long id){
        if(orderUserService.payOrder(id)){
            return R.ok();
        }else return R.error(500,"订单支付失败，原因为库存不足或支付逾期，请重新下单。");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        orderUserService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    //用户下单后生成订单
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody CreateOrderDto orderdto) {
        orderUserService.createOrder(orderdto);
        return R.ok();
    }
    //取消订单
    @PostMapping("/cancelOrder/{id}")
    public R cancelOrder(@PathVariable("id") Long id) {
        orderUserService.cancelOrder(id);
        return R.ok();
    }
}
